package myStack;

class LinkNode<T>            //链栈结点泛型类
{
    T data;
    LinkNode<T> next;

    public LinkNode()            //构造方法
    {
        next = null;
    }

    public LinkNode(T d)        //重载构造方法
    {
        data = d;
        next = null;
    }
    
}


public class MyLinkedStack<T> {
    LinkNode<T> head;            //存放头结点

    public MyLinkedStack()        //构造方法
    {
        head = new LinkNode<T>();        //创建头结点
        head.next = null;            //设置为空栈
    }

    //栈的基本运算算法
    public boolean empty()    //判断栈是否为空
    {
        return head.next == null;
    }

    public void push(T e)            //元素e进栈
    {
        LinkNode<T> s = new LinkNode<T>(e);    //新建结点s
        s.next = head.next;            //将结点s插入到表头
        head.next = s;
    }

    public T pop()            //出栈操作
    {
        if (empty())
            throw new IllegalArgumentException("栈空");
        T e = (T) head.next.data;    //取首结点值
        head.next = head.next.next;    //删除原首结点
        return e;
    }

    public T peek()            //取栈顶元素操作
    {
        if (empty())
            throw new IllegalArgumentException("栈空");
        T e = (T) head.next.data;        //取首结点值
        return e;
    }

    public static MyLinkedStack<Integer>
    Reverse(MyLinkedStack<Integer> st) {
        int[] a = new int[1000];
        int i = 0;
        while (!st.empty())        //将出栈的元素放到数组a中
        {
            a[i] = st.pop();
            i++;
        }
        for (int j = 0; j < i; j++)        //将数组a的所有元素进栈
            st.push(a[j]);
        return st;
    }

}
